<?php
namespace app\shouyin\admin;
use app\system\admin\Admin;
use app\shouyin\model\SubscribeProject as SubscribeProjectModel;
use app\system\model\systemUser as systemUserModel;
use app\product\model\Project as ProjectModel;
use think\Db;
class SubscribeProject extends Admin
{
    protected $hisiModel = 'SubscribeProject';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名
    protected function initialize()
    {
        parent::initialize();
        if ($this->request->action() != 'index' && !$this->request->isPost()) {
            $where = [];

            $where['shop_id'] = session('shop_id');
            $this->assign('sub_id',$this->request->param('sub_id'));

            // 顾问
            $admins = systemUserModel::where($where)->all();
            $advisers = [];
            $technician = [];
            // halt($admins);
            foreach ($admins as $key => $value) {
                if($value['type'] == '顾问'){
                    $advisers[] = $value;
                }else if($value['type'] == '美容师'){
                    $technician[] = $value;
                }
            }

            $this->assign('advisers', systemUserModel::getSelect($advisers));
            $this->assign('technicians', $technician);
            // $projects = ProjectModel::all();
            // halt( ProjectModel::all());
            $this->assign('projects', ProjectModel::all());
            // $this->assign('projects', ProjectModel::getSelect(ProjectModel::all()));
        }
    }

    public function editSettlement()
    {
        if ($this->request->isPost()) {// 数据验证

            $postData = $this->request->post();

            if ($this->hisiValidate) {

                if (strpos($this->hisiValidate, '\\') === false ) {

                    if (defined('IS_PLUGINS')) {
                        $this->hisiValidate = 'plugins\\'.$this->request->param('_p').'\\validate\\'.$this->hisiValidate;
                    } else {
                        $this->hisiValidate = 'app\\'.$this->request->module().'\\validate\\'.$this->hisiValidate;
                    }

                }

                if ($this->hisiEditScene) {
                    $this->hisiValidate = $this->hisiValidate.'.'.$this->hisiEditScene;
                }

                $result = $this->validate($postData, $this->hisiValidate);
                if ($result !== true) {
                    return $this->error($result);
                }

            }
        }

        $where = [];
        if ($this->hisiModel) {// 通过Model更新

            $model = $this->model();

            $pk = $model->getPk();
            $id = $this->request->param($pk);

            $where[]= [$pk, '=', $id];
            $where  = $this->getRightWhere($where);

            if ($this->request->isPost()) {

                if ($model->save($postData, $where) === false) {
                    return $this->error($model->getError());
                }

                return $this->success('保存成功', '');
            }

            $formData = $model->where($where)->find();

        } else if ($this->hisiTable) {// 通过Db更新

            $db = Db::name($this->hisiTable);
            $pk = $db->getPk();
            $id = $this->request->param($pk);

            $where[]= [$pk, '=', $id];
            $where  = $this->getRightWhere($where);

            if ($this->request->isPost()) {

                if (!$db->where($where)->update($postData)) {
                    return $this->error('保存失败');
                }

                return $this->success('保存成功', '');
            }

            $formData = $db->where($where)->find();

        } else {

            return $this->error('当前控制器缺少属性（hisiModel、hisiTable至少定义一个）');

        }

        if (!$formData) {
            return $this->error('数据不存在或没有权限');
        }

        $this->assign('formData', $formData);

        $template = $this->request->param('template', 'form');

        return $this->fetch();
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            // $page       = $this->request->param('page/d', 1);
            // $limit      = $this->request->param('limit/d', 15);
            $where['sub_id'] = $this->request->param('sub_id');
            // halt($where);
            $data['data'] = SubscribeProjectModel::where($where)
                            // ->page($page)
                            // ->limit($limit)
                            ->order('id desc')
                            ->select();
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['adviser'] = Db::name('system_user')->where('id',$data['data'][$key]['adviser_id'])->column('nick');
                $data['data'][$key]['technician'] = Db::name('system_user')->where('id','in',$data['data'][$key]['technician_ids'])->column('nick');
                $data['data'][$key]['projects'] = Db::name('project')->where('id',$data['data'][$key]['project_id'])->column('name');
            }
            $data['count'] = SubscribeProjectModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        // return $this->fetch();
    }
}
